const db = require("../db/index.js");
// 导入bcrypt加密中间件
const bcrypt = require("bcrypt");
// 导入node.js的crypto库生成uuid
const crypto = require('crypto')
// 导入fs处理文件
fs = require('fs')
// 上传用户图片接口
exports.uploadAvatar = (req,res)=>{
	// 生成唯一标识
	const onlyId = crypto.randomUUID()
	let oldName = req.files[0].filename;
	let newName = Buffer.from(req.files[0].originalname, 'latin1').toString('utf8')
	fs.renameSync('./public/upload/' + oldName, './public/upload/' + newName)
	const sql = 'insert into image set ?'
	db.query(sql, {
		image_url: `http://127.0.0.1:3007/upload/${newName}`,
		onlyId
	}, (err, result) => {
		if (err) return res.cc(err)
		res.send({
			onlyId,
			status: 0,
			url: 'http://127.0.0.1:3007/upload/' + newName
		})
	})
}
// 绑定账号接口
exports.bindAccount = (req,res)=>{
	const {
		account,
		onlyId,
		url
	} = req.body
	const sql = "update image set account = ? where onlyId = ?"
	db.query(sql,[account,onlyId],(err,result)=>{
		if(err) return res.cc(err)
		if(result.affectedRows == 1){
			const sql1 = "update users set image_url = ? where account = ?"
			db.query(sql1,[url,account],(err,result)=>{
				if(err) return res.cc(err)
				res.send({
					status:0,
					message:"修改成功"
				})
			})
		}
		
	})
}

// 获取用户信息
exports.getUserInfo = (req,res)=>{
	const sql = "select * from users where id = ?"
	db.query(sql,req.body.id,(err,relust)=>{
		if(err) return res.cc(err);
		res.send(relust)
	})
}

// 修改姓名
exports.changeName = (req,res)=>{
	const sql = "update users set name = ? where id = ?"
	const {
		name,
		id
	} = req.body
	db.query(sql,[name,id],(err,result)=>{
		if(err) return res.cc(err);
		res.send({
			status:0,
			message:"修改成功"
		})
	})
}

// 修改性别
exports.changeSex = (req,res)=>{
	const sql = "update users set sex = ? where id = ?"
	const {
		sex,
		id
	} = req.body
	db.query(sql,[sex,id],(err,result)=>{
		if(err) return res.cc(err);
		res.send({
			status:0,
			message:"修改成功"
		})
	})
}
// 修改邮箱
exports.changeEmail = (req,res)=>{
	const sql = "update users set email = ? where id = ?"
	const {
		email,
		id
	} = req.body
	db.query(sql,[email,id],(err,result)=>{
		if(err) return res.cc(err);
		res.send({
			status:0,
			message:"修改成功"
		})
	})
}

// 修改密码
exports.changePassword = (req,res)=>{
	const sql = "select password from users where id = ?"
	db.query(sql,req.body.id,(err,ruselt)=>{
		if(err) return res.cc(err);
		// 加密比较
		const compareResult = bcrypt.compareSync(req.body.oldPassword,ruselt[0].password);
		// 如果不相等
		if(!compareResult){
			return res.send({
				status:1,
				message:"原密码不正确"
			})
		}
		// 对新密码进行加密
		req.body.newPassword = bcrypt.hashSync(req.body.newPassword,10)
		const sql1 = "update users set password = ? where id = ?"
		db.query(sql1,[req.body.newPassword,req.body.id],(err,result)=>{
			if(err) res.cc(err);
			res.send({
				status:0,
				message:"修改成功"
			})
		})
	})
}

// 验证账号与邮箱是否一致 email account
exports.verifyAccountAndEmail = (req,res)=>{
	const {
		account,
		email
	} = req.body;
	const sql = "select * from users where account = ? "
	db.query(sql,account,(err,result)=>{
		if(err) return res.cc(err);
		if(email == result[0].email){
			res.send({
				status:0,
				message:"查询成功",
				id:result[0].id
			})
		}else{
			res.send({
				status:1,
				message:"查询失败"
			})
		}
	})
}

// 登录页设置密码 password id
exports.changePasswordInLogin = (req,res)=>{
	const user = req.body;
	// 对密码进行加密
	user.newPassword = bcrypt.hashSync(req.body.newPassword,10)
	const sql = "update users set password = ? where id = ?";
	db.query(sql,[user.newPassword,user.id],(err,result)=>{
		if(err) return res.cc(err);
		res.send({
			status:0,
			message:"修改成功"
		})
	})
}

// ----------------------------- 用户管理
// 添加管路员
exports.createAdmin = (req,res)=>{
	const {
		account,
		password,
		name,
		sex,
		department,
		email,
		identity
	} = req.body
	// 先查询用户是否存在
	const sql = "select * from users where account = ?"
	db.query(sql,account,(err,result)=>{
		if(result.length > 0){
			res.send({
				status:1,
				message:"账号已经存在"
			})
		}
		// 不存在，对密码进行加密
		// 使用加密中间件bcrypt.js
		// 第一个是传入的密码，第二个是加密的长度
		hashpassword = bcrypt.hashSync(password,10)
		const sql1 = "insert into users set ?"
		const create_time = new Date()	//获取创建时间
		db.query(sql1,{
			account,
			password:hashpassword,
			name,
			sex,
			department,
			email,
			identity,
			// 创建时间
			create_time,
			// 初始化未冻结状态
			status:0
		},(err,result)=>{
			// affectedRows为影响的行数，如果插入失败，那么没有影响到行数，也就是行数不为1
			if(result.affectedRows !== 1){
				return res.send({
					status:1,
					message:"添加管理员失败"
				})
			}else{
				return res.send({
					status:0,
					message:"添加管理员成功"
				})
			}
		})
	})
}
// 获取管理员列表
exports.getAdminList = (req,res)=>{
	const page = (req.body.page - 1) * req.body.size
	const size = req.body.size
	const sql = `select * from users where identity = ? limit ${page},${size}`
	db.query(sql,req.body.identity,(err,result)=>{
		if(err) return res.cc(err);
		let data = result
		const sql1 = 'select * from users where identity = ?'
		db.query(sql1,req.body.identity,(err,result)=>{
			if(err) return res.cc(err);
			res.send({
				status:0,
				result:data,
				length:result.length
			})
		})
		
	})
}
// 编辑管路员信息
exports.editAdmin = (req,res) => {
	const {
		id,
		name,
		sex,
		email,
		department,
	} = req.body
	const date = new Date();
	// 修改的内容
	const updateContent = {
		id,
		name,
		sex,
		email,
		department,
		update_time:date
	}
	const sql = "update users set ? where id = ?"
	db.query(sql,[updateContent,updateContent.id],(err,result)=>{
		if(err) return res.cc(err);
		return res.send({
			status:0,
			message:"修改管路员信息成功"
		})
	})
}
// 对管理员进行降职的操作
exports.changeIdentityToUser = (req,res) => {
	const sql = "update users set identity = '用户' where id = ?"
	db.query(sql,req.body.id,(err,result)=>{
		if(err) return res.cc(err);
		return res.send({
			status:0,
			message:"降级成功"
		})
	})
}
// 对用户进行赋权的操作
exports.changeIdentityToAdmin = (req,res) => {
	const sql = "update users set identity = ? where id = ?"
	db.query(sql,[req.body.identity,req.body.id],(err,result)=>{
		if(err) return res.cc(err);
		return res.send({
			status:0,
			message:"赋权成功"
		})
	})
}
// 通过账号对用户进行搜索
exports.searchUser = (req,res) => {
	const sql = "select * from users where account = ?"
	db.query(sql,req.body.account,(err,result)=>{
		if(err) return res.cc(err);
		return res.send({
			status:0,
			result
		})
	})
}
// 冻结用户 把status设置为 1
exports.banUser = (req,res) => {
	const status = 1
	const sql = "update users set status = ? where id = ?"
	db.query(sql,[status,req.body.id],(err,result)=>{
		if(err) return res.cc(err);
		return res.send({
			status:0,
			message:"冻结成功"
		})
	})
}
// 解冻用户 把status设置为 0
exports.hotUser = (req,res) => {
	const status = 0
	const sql = "update users set status = ? where id = ?"
	db.query(sql,[status,req.body.id],(err,result)=>{
		if(err) return res.cc(err);
		return res.send({
			status:0,
			message:"解冻成功"
		})
	})
}
// 获取冻结列表
exports.getBanList = (req,res)=>{
	const sql = "select * from users where status = '1'"
	db.query(sql,(err,result)=>{
		if(err) return res.cc(err);
		res.send({
			status:0,
			result
		})
	})
}
// 删除用户 id account
exports.deleteUser = (req,res)=>{
	const sql = "delete from users where id = ?"
	db.query(sql,req.body.id,(err,result)=>{
		if(err) return res.cc(err);
		// 删除用户图片
		const sql1 = "delete from image where account = ?"
		db.query(sql1,req.body.account,(err,result)=>{
			if(err) return res.cc(err);
			res.send({
				status:0,
				message:"删除用户成功"
			})
		})
	})
}